// 如果需要手动映射type和组件的关系，请在这里配置
const viewRegisterMap = {
    'straight-line': 'view-line',
    'vertical-line': 'view-line',
    'chart-line': 'view-chart',
    'chart-line-step': 'view-chart',
    'chart-bar': 'view-chart',
    num: 'view-text',
};

// 优先匹配map，否则将自动匹配
export function parseViewName(component) {
    let viewName = viewRegisterMap[component.type];
    if (!viewName) {
        viewName = 'view-' + component.type;
    }
    return viewName;
}

// 判断两个值大小
export function judgeSize(formula, value1, value2) {
    value1 = Number(value1) || 0;
    value2 = Number(value2) || 0;
    let isTrue = false;
    if (formula == '>') {
        if (value1 > value2) {
            isTrue = true;
        }
    } else if (formula == '>=') {
        if (value1 >= value2) {
            isTrue = true;
        }
    } else if (formula == '=') {
        if (value1 == value2) {
            isTrue = true;
        }
    } else if (formula == '<=>') {
        if (value1 <= value2) {
            isTrue = true;
        }
    } else if (formula == '<>') {
        if (value1 < value2) {
            isTrue = true;
        }
    } else if (formula == '!=') {
        if (value1 != value2) {
            isTrue = true;
        }
    }
    return isTrue;
}

// 判断是否处在自定义范围内
export function isInCustomRange(value1, start, end) {
    value1 = Number(value1) || 0;
    start = Number(start) || 0;
    end = Number(end) || 0;
    let isTrue = false;
    if (value1 >= start && value1 <= end) {
        isTrue = true;
    }
    return isTrue;
}

// 判断是否不在自定义范围内
export function isNotInCustomRange(value1, start, end) {
    value1 = Number(value1) || 0;
    start = Number(start) || 0;
    end = Number(end) || 0;
    let isTrue = false;
    if (value1 < start || value1 > end) {
        isTrue = true;
    }
    return isTrue;
}

function add0(m) {
    return m < 10 ? '0' + m : m;
}

// 获取当前时间
export function getNowTime() {
    // 年
    let year = new Date().getFullYear();
    // 月份是从0月开始获取的，所以要+1;
    let month = new Date().getMonth() + 1;
    // 日
    let day = new Date().getDate();
    // 时
    let hour = new Date().getHours();
    // 分
    let minute = new Date().getMinutes();
    // 秒
    let second = new Date().getSeconds();
    return year + '-' + add0(month) + '-' + add0(day) + ' ' + add0(hour) + ':' + add0(minute) + ':' + add0(second);
}

// 获取当前时间前2小时
export function getTime(val) {
    var frontOneHour = new Date(new Date().getTime() - val * 60 * 60 * 1000);
    // 年
    let year = frontOneHour.getFullYear();
    // 月份是从0月开始获取的，所以要+1;
    let month = frontOneHour.getMonth() + 1;
    // 日
    let day = frontOneHour.getDate();
    // 时
    let hour = frontOneHour.getHours();
    // 分
    let minute = frontOneHour.getMinutes();
    // 秒
    let second = frontOneHour.getSeconds();
    return year + '-' + add0(month) + '-' + add0(day) + ' ' + add0(hour) + ':' + add0(minute) + ':' + add0(second);
}

// 数据引擎说明
export function getEchartExplain() {
    return '<p><span style="color: rgb(255, 153, 0);">响应示例</span><span style="color: rgb(0, 102, 204);">-</span><a href="https://www.isqqw.com/" rel="noopener noreferrer" target="_blank" style="background-color: rgb(255, 255, 255); color: rgb(0, 102, 204);">echartData</a></p><p>{</p><p>&nbsp;"msg": "操作成功",</p><p>&nbsp;"code": 200,</p><p>&nbsp;"<span style="color: rgb(0, 102, 204);">data</span>": {</p><p>&nbsp;&nbsp;"<span style="color: rgb(0, 102, 204);">xdata</span>": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],</p><p>&nbsp;&nbsp;"<span style="color: rgb(0, 102, 204);">ydata</span>": [150, 230, 224, 218, 135, 147, 260]</p><p>&nbsp;&nbsp;}</p><p>}</p><p><span style="color: rgb(255, 153, 0);">代码视图</span></p><p>option = {</p><p>&nbsp;xAxis: {</p><p>&nbsp;&nbsp;	type: "category",</p><p>&nbsp;	data: <a href="https://www.isqqw.com/" rel="noopener noreferrer" target="_blank" style="color: rgb(0, 102, 204); background-color: rgb(255, 255, 255);">echartData</a><span style="color: rgb(0, 102, 204);">.data.xdata</span></p><p>&nbsp;},</p><p>&nbsp;yAxis: {</p><p>&nbsp;&nbsp;	type: "value"</p><p>&nbsp;},</p><p>&nbsp;series: [</p><p>&nbsp;&nbsp;{</p><p>&nbsp;&nbsp;&nbsp;	data: <a href="https://www.isqqw.com/" rel="noopener noreferrer" target="_blank" style="color: rgb(0, 102, 204); background-color: rgb(255, 255, 255);">echartData</a><span style="color: rgb(0, 102, 204);">.data.ydata</span>,</p><p>&nbsp;&nbsp;&nbsp;	type: "line"</p><p>&nbsp;&nbsp;}</p><p>&nbsp;]</p><p>};</p>';
}

/**
 * 生成随机整数
 * @param {*} min
 * @param {*} max
 */
export function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

/**
 * 生成随机浮点数
 * @param {*} min
 * @param {*} max
 * @param {*} dp 小数点位数
 */
export function getRandomFloat(min, max, dp) {
    return parseFloat((Math.random() * (max - min) + min).toFixed(dp));
}

/**
 * 生成随机元素,值1
 * @param {*} str  值1,值2,值3......，中间用英文逗号隔开
 */
export function getRandomString(str) {
    const arr = str.split(',');
    return arr[Math.floor(Math.random() * arr.length)];
}
